﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using MySql.Data.MySqlClient;
using System.Data;
using MaxLeaf.Common;
using MaxLeaf.DB;
using System.Transactions;
using System.Data.Common;
using System.Diagnostics;


namespace MaxLeaf.WebService
{
    /// <summary>
    /// MaxLeafWebService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://MaxLeaf.WebService/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class MaxLeafWebService : System.Web.Services.WebService
    {
        [System.Web.Services.Protocols.SoapDocumentMethod(OneWay = true)]
        
        [WebMethod]
        public void TestOneWay(string strMsg)
        {
            ServerInfo server = ServerInfo.GetK3MasterServer();

            if (server.CheckingLog())
            {
                return;
            }


            try
            {


                System.Threading.Thread.Sleep(60 * 1000);
                LogManager.Debug("TestOneWay" + strMsg);
            }
            catch (Exception ex)
            {
                LogManager.Error(ex);
            }
            finally
            {
                server.CheckedLog();
            }


            return;
        }


        [WebMethod]
        public string GetOnHand(string strDBName, string strFitemID, string strMDCFItemID, string strUnitFItemID)
        {
            double strReturn = new MaxLeaf.DAL.GetOnHand().GetQtyOnHand(strDBName, strFitemID, strMDCFItemID, strUnitFItemID);
            return strReturn.ToString();
        }


        [WebMethod]
        public string GoodsInUsed( string strFitemID )
        {
            int strReturn = new MaxLeaf.DAL.GoodsInUsed().CheckGoodsInUsed(strFitemID);
            return strReturn.ToString();
        }


        [WebMethod]
        public string Test(string strServerName)
        {

            ServerInfo server = ServerInfo.GetServer(strServerName);

            DataRow dr = new DBAccess(server).GetDataFirstRow(GetTestSql(server.Type));

            if (dr == null)
            {
                return string.Empty;
            }
            return dr[GetTestField(server.Type)].ToString();
        }


        [WebMethod]
        public string TestTrans()
        {
            ServerInfo server = ServerInfo.GetServer("K3master");
            using (DBAccess dbCon = new DBAccess(server))
            {
                dbCon.BeginTrans();

                string strSql = @"insert into mLog(logtype,keyvalue,logstatus)
                values('T_icitem','3093','0')";
                dbCon.ExecuteSql(strSql);

                strSql = "select * from mLog";
                DataRow dr = dbCon.GetDataFirstRow(strSql);

                dbCon.RollbackTrans();
            }
            //            using (TransactionScope ts = new TransactionScope(  ))
            //            {


            //                    ServerInfo server = ServerInfo.GetServer("K3master");
            //                    using (DBAccess dbCon = new DBAccess(server))
            //                    {
            //                        string strSql = @"insert into mLog(logtype,keyvalue,logstatus)
            //values('T_icitem','3093','0')";
            //                        dbCon.ExecuteSql( strSql);

            //                    }
            //                    server = ServerInfo.GetServer("B01");
            //                    using (DBAccess dbCon = new DBAccess(server))
            //                    {
            //                        string strSql = @"insert into mLog(logtype,keyvalue,logstatus)
            //values('T_icitem','3093','0')";
            //                        dbCon.ExecuteSql(strSql);

            //                    }
            //                    ts.Complete();


            //            }
            return "aaa";
        }

        //[WebMethod]
        //public string GetOnHand(string strServerName, string strDBName, string strCustFitemID, string strItemItemID, string strUnit)
        //{
        //    //ServerInfo server = ServerInfo.GetServer(strServerName);
        //    //server.Database = strDBName;
        //    ServerInfo server = ServerInfo.GetK3ServerByDBName(strDBName);
        //    DBAccess dbCon = new DBAccess(server);
        //    string strReturn = new MaxLeaf.DAL.UpdateOnHandOverNightDAL().GetOnHand(dbCon, strDBName, strCustFitemID, strItemItemID,strUnit); 
            
        //    return strReturn;
        //}

        [System.Web.Services.Protocols.SoapDocumentMethod(OneWay = true)]
        [WebMethod]
        public void CheckSCPLog(string strServerName)
        {
            try
            {
                LogManager.Debug("CheckSCPLog" + strServerName);
                ServerInfo server = ServerInfo.GetServer(ServerType.SCP, strServerName);

                if (server == null)
                {
                    LogManager.Error(new MaxLeafException(string.Format("[CheckSCPLog] Server {0} 不存在", strServerName)));
                    return;
                }
                CheckLog(server);
            }
            catch (Exception ex)

            { LogManager.Error(ex); }
        }

        [System.Web.Services.Protocols.SoapDocumentMethod(OneWay = true)]
        [WebMethod]
        public void CheckWebLog(string strServerName)
        {
            try
            {
                LogManager.Debug("CheckWebLog" + strServerName);
                ServerInfo server = ServerInfo.GetServer(ServerType.Web, strServerName);

                if (server == null)
                {
                    LogManager.Error(new MaxLeafException(string.Format("[CheckWebLog] Server {0} 不存在", strServerName)));
                    return;
                }
                CheckLog(server);
            }
            catch (Exception ex)

            { LogManager.Error(ex); }
        }

        [System.Web.Services.Protocols.SoapDocumentMethod(OneWay = true)]
        [WebMethod]
        public void CheckK3Log(string strDBName)
        {
            try
            {
                LogManager.Debug("CheckK3Log" + strDBName);
                ServerInfo server = ServerInfo.GetK3ServerByDBName(strDBName);

                if (server == null)
                {
                    LogManager.Error(new MaxLeafException(string.Format("[CheckK3Log] Server {0} 不存在", strDBName)));
                    return;
                }
                CheckLog(server);
            }
            catch (Exception ex)

            { LogManager.Error(ex); }
        }

        private void CheckLog(ServerInfo server)
        {

            if (server.CheckingLog())
            {
                return;
            }

            try
            {

                new DAL.DALFactory().CheckLog(server);
            }
            catch (Exception ex)
            {
                LogManager.Error(ex);
            }
            finally
            {
                server.CheckedLog();
            }
            return;
        }

        private string GetTestField(MaxLeaf.Common.ServerType serverType)
        {
            if (serverType == ServerType.K3)
            {
                return "Fname";
            }
            else
            {
                return "goods_name";
            }
        }

        private string GetTestSql(MaxLeaf.Common.ServerType serverType)
        {
            if (serverType == ServerType.K3)
            {
                return "Select top 1 * from t_Item    order by fitemid desc ";
            }
            else
            {
                return "Select * from goods_main limit 1,1";
            }
        }

    }
}
